
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class Login extends JFrame implements ActionListener, ItemListener{
	JPanel center, center1, center2, south;
	 JLabel title, la_passwd;
	 JPasswordField t_passwd;
	 JButton login;
	 String driver="com.mysql.jdbc.Driver";
	 String url="jdbc:mysql://localhost:3306/gage";
	 String uid="root";
	 String upasswd="apmsetup";
	 
	 Connection con;
	 PreparedStatement pstmt;
	 ResultSet rs;
 
	

	public Login(){
		
		center = new JPanel();
		  center1 = new JPanel();
		  center2 = new JPanel();
		  south = new JPanel();
		  title = new JLabel("관리자 인증");
		  la_passwd= new JLabel("P/W  ");
		  t_passwd = new JPasswordField(20);
		  login = new JButton("Login");
		  
		  add(title, BorderLayout.NORTH);
		  
		  center.setLayout(new GridLayout(2,1));
		    
		  center2.add(la_passwd);
		  center2.add(t_passwd);
		  
		  center.add(center2);    
		  add(center);
		  
		  south.add(login);
		  add(south, BorderLayout.SOUTH);
		    
		  login.addActionListener(this);
		  
		  setBounds(200,100,300,150); 
		  setVisible(true);
		  
		  /*DB 접속시도*/
		  if(connect()){
		   showMsg("DB연결!");
		   t_passwd.requestFocus(); //커서올려놓기   
		  }else{
		   showMsg("DB연결실패");
		  }
		  
		 }
		 
		 public boolean connect() {
		  boolean result=false;
		  try {
		   Class.forName(driver);
		   con = DriverManager.getConnection(url,uid,upasswd);
		   if(con!=null){
		    result= true;
		   }else{
		    result= false;
		   }
		  } catch (ClassNotFoundException e) {  
		   e.printStackTrace();
		  } catch (SQLException e) {
		   e.printStackTrace();
		  }
		  return result;  
		 }
		 
		 public void loginCheck(){
		  
		  char[] ch=t_passwd.getPassword();
		  String passStr=new String(ch);
		  if(passStr.length()==0){
		   showMsg("비밀번호를 입혁하지 않았습니다.");
		   t_passwd.requestFocus();
		   return;
		  }

		  String sql="select * from admin where pw=?";
		  try {
		   pstmt=con.prepareStatement(sql);
		   pstmt.setString(1, passStr);
		   rs=pstmt.executeQuery();
		   
		   if(rs.next()){
		    showMsg("로그인되었습니다.");
        Member m = new Member(this);
		    this.setVisible(false);
		 
		   }else{
		    showMsg("비밀번호가 맞지 않습니다.");
		   }
		  } catch (SQLException e) {
		   e.printStackTrace();
		  }
		  
		 }
		 
		 
		 public void actionPerformed(ActionEvent e) {
		  loginCheck();
		 }
		 
		 
		 public void showMsg(String msg){
		  JOptionPane.showMessageDialog(getParent(), msg);
		  
		 }
		 
		 public static void main(String[] args) {
		  new Login();
		 }

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			
		}
		}
